<?php
/**
 * tis-tis 后台模板文件
 *
 * @copyright (c)2010 昆明力行软件  保留所有权利。
 * @link http://www.tis-tis.com
 * @Author: XiangJie
 * @version: 1.0.0
 */

require_once '../init.php';
require_once TIS_ROOT.'/internal/common.php';
require_once TIS_ROOT.'/admin/internal/tools.php';
require_once TIS_ROOT.'/internal/mysql.class.php';
require_once TIS_ROOT.'/admin/internal/smarty_config.php';

$act=isset($_REQUEST['act'])?$_REQUEST['act']:'list';
$DB = MySql::getInstance();
$smarty->assign('tpl_path','templates/default');

if ($act == 'list')
{
	$curr_template = get_template();

	$available_templates = array();

	$template_dir        = @opendir('../templates/');

	while ($file = readdir($template_dir))

	{

		if ($file != '.' && $file != '..' && is_dir('../templates/' . $file) && $file != '.svn' && $file != 'index.htm')

		{

			$available_templates[] = get_template_info($file);

		}

	}
	@closedir($template_dir);

	$smarty->assign('curr_template',       get_template_info($curr_template));
	$smarty->assign('templates', $available_templates);
	$smarty->assign('title',getPageTitle("设置TIS模板"));

	$smarty->display('template.tpl');
}
/*安装模板*/
if ($act == 'install')
{
	$value = $_REQUEST['value'];
	if (!$value) {
		redirectURL('template.php?act=list', "设置的值不能为空！");
	}

	/*保存设置*/
	setTemplate($value);

	redirectURL('template.php?act=list', "模板设置成功！");
}

/**

* 获得模版信息

*

* @access  private

* @param   string      $template_name      模版名

* @return  array

*/

function get_template_info($template_name) {
	if (empty($template_name)) {
		return;
	}

	$temp_info = array();
	$temp_info['value'] = $template_name;

	if (file_exists('../templates/' . $template_name . "/images/screenshot.png")) {
		$temp_info['screenshot'] = '../templates/' . $template_name . "/images/screenshot.png";
	} else {
		$temp_info['screenshot'] = "../images/screenshot.png";
	}
	/*获取css文件中定义的相关模板信息*/
	$cpath = '../templates/' . $template_name . '/style.css';

	if (file_exists($cpath) && !empty($template_name)) {
		$arr = array_slice(file($cpath), 0, 9);

		$template_name      = explode(': ', $arr[1]);
		$template_url       = explode(': ', $arr[2]);
		$template_desc      = explode(': ', $arr[3]);
		$template_version   = explode(': ', $arr[4]);
		$template_author    = explode(': ', $arr[5]);
		$author_url         = explode(': ', $arr[6]);
		$logo_filename      = explode(': ', $arr[7]);

		$temp_info['name']       = isset($template_name[1]) ? trim($template_name[1]) : '';
		$temp_info['url']        = isset($template_url[1]) ? trim($template_url[1]) : '';
		$temp_info['desc']       = isset($template_desc[1]) ? trim($template_desc[1]) : '';
		$temp_info['version']    = isset($template_version[1]) ? trim($template_version[1]) : '';
		$temp_info['author']     = isset($template_author[1]) ? trim($template_author[1]) : '';
		$temp_info['author_url'] = isset($author_url[1]) ? trim($author_url[1]) : '';
		$temp_info['logo']       = isset($logo_filename[1]) ? trim($logo_filename[1]) : '';
	}  else {
		$temp_info['name']       = '';
		$temp_info['url']        = '';
		$temp_info['desc']       = '';
		$temp_info['version']    = '';
		$temp_info['author']     = '';
		$temp_info['author_url'] = '';
		$temp_info['logo']       = '';
	}

	return $temp_info;

}

/**
 * 设置模板。
 *
 * @param $value 设置的模板值
 * @author XiangJie
 */
function setTemplate($value) {
	global $DB;
	if (!file_exists('../templates/' . $value)) {
        redirectURL('template.php?act=list', "设置的模板不存在，请重新设置！");
	}
	$sql = "update ".$DB->table(shop_options)." set value='$value' where keyword='template';";
	$DB->query($sql);
}

?>